<template>
  <div class="addUser">
    <h3 class="title">添加用户信息</h3>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="150px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="ruleForm.userName"></el-input>
      </el-form-item>
       <el-form-item label="密码" prop="passWord">
        <el-input type="passWord" autocomplete="off" v-model="ruleForm.passWord"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="ruleForm.sex">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input v-model="ruleForm.age"></el-input>
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-date-picker
          v-model="ruleForm.birthday"
          value-format="yyyy-MM-dd"
          type="date"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>

        <el-upload
        class="avatar-uploader"
        :data="data"
        :action="action"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
      >
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        <div class="el-upload__text">点击上传头像</div>
      </el-upload>

      <el-form-item>
        <el-button type="primary" :plain="true" @click="submitForm('ruleForm')">添加</el-button>
        <el-button type="info" :plain="true" @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import adminRequest from "../request/admin";
import axios from "../request/axios";
export default {
  data() {
    var userName = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请填写客户名称"));
      } else {
        return callback();
      }
    };
    var age = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写年龄"));
      } else {
        return callback();
      }
    };
      var birthday = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写您的生日"));
      } else {
        return callback();
      }
    };
    var sex = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请选择性别"));
      } else {
        return callback();
      }
    };
    var phone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写联系电话"));
      } else {
        return callback();
      }
    };
    var passWord = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (/^[a-z0-9]{5,12}$/.test(value)) {
          callback();
        } else {
          callback(new Error("密码必须为字母开头6-12位数字字母组成"));
        }
      }
    };
  
    return {
       data: {
        dataId: Math.random().toString(36).substr(3, 6) + Date.now().toString(),
      },
      action: axios.defaults.baseURL + "centre/centre/file/upload",
      imageUrl: "",

      ruleForm: {
        userName: "",
        sex: "",
        age: "",
        phone: "",
        passWord: "",
        birthday: ""
      },
      rules: {
        userName: [{ validator: userName, trigger: "blur" }],
        phone: [{ validator: phone, trigger: "blur" }],
        sex: [{ validator: sex, trigger: "blur" }],
        age: [{ validator: age, trigger: "blur" }],
        passWord: [{ validator: passWord, trigger: "blur" }],
        birthday: [{ validator: birthday, trigger: "blur" }],
      }
    };
  },

  methods: {
     handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },

    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },


    // 添加
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          adminRequest
            .addUser({
              userName: this.ruleForm.userName,
              passWord: this.ruleForm.passWord,
              phone: this.ruleForm.phone,
              sex: this.ruleForm.sex,
              age: this.ruleForm.age,
              birthday: this.ruleForm.birthday,
              photo: this.data.dataId
            })
            .then(res => {
              if (res.data.success == true) {
                this.$message.success("添加成功!");
                this.resetForm();
              } else {
                this.$message.error(res.data.message);
              }
            });
        } else {
          this.$message.error("请检查信息是否完整！");
          return false;
        }
      });
    },
    // 清空表单数据
    resetForm() {
      this.$refs.ruleForm.resetFields();
    }
  },

  created() {}
};
</script>

<style lang="scss" scoped>
.addUser {
  overflow: hidden;

  ::v-deep .title {
    text-align: center;
    font-size: 20px;
  }
  ::v-deep .demo-ruleForm {
    width: 600px;
    margin: 30px auto;
  }
  ::v-deep .el-input__inner {
    height: 45px;
    font-size: 16px;
  }
  ::v-deep .el-form-item__label {
    font-size: 16px;
    position: relative;
    top: 10px;
  }
  ::v-deep .el-form-item__content {
    display: flex;
    margin-top: 10px;
    height: 50px;
  }
  ::v-deep .el-form-item__error {
    font-size: 16px;
  }

  ::v-deep .el-select-dropdown__item {
    display: flex;
    justify-content: center;
  }
  ::v-deep .el-radio-group {
    width: 100px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  ::v-deep .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    margin-left: 150px;
    height: 178px;
  }
  ::v-deep .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  ::v-deep .avatar-uploader-icon {
    font-size: 24px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  ::v-deep .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
  ::v-deep .el-upload__text {
    color: #8c939d;
    position: relative;
    bottom: 50px;
    font-size: 14px;
  }
}
</style>